﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmartMD.DataAccess.Interface.Dao;
using SmartMD.DataModel.Entity;
using System.Collections.ObjectModel;
using System.Data.OleDb;
using System.Xml;
using SmartMD.DataAccess.Service;
using System.Data;  

namespace SmartMD.DataAccess.Oracle.DaoImpl
{
    public class ExamItemValueDaoImpl : BaseGenericDaoImpl, IExamItemValueDao
    {
        private const int MAX_INDICE_NUM = 25;

        public bool DeleteObject(Object entity)
        {
            throw new NotImplementedException();
        }

        public bool SaveOrUpdateObject(Object entity)
        {
            throw new NotImplementedException();
        }
        public bool saveExamItemValues(ObservableCollection<ExamItemValue> itemValueList)
        {

            ExamItemValue itemValue;

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < itemValueList.Count; i++)
            {
                itemValue = itemValueList[i];

                sb.Clear();
                sb.Append(" insert into");
                sb.Append(" DUO_EXAMITEM_VALUE (INDICEVALUEID, SOURCEID, ITEMID,");
                sb.Append(" INDICE1, INDICE2, INDICE3, INDICE4, INDICE5, INDICE6, INDICE7, INDICE8, INDICE9, INDICE10,");
                sb.Append(" INDICE11, INDICE12, INDICE13, INDICE14, INDICE15, INDICE16, INDICE17, INDICE18, INDICE19, INDICE20,");
                sb.Append(" INDICE21, INDICE22, INDICE23, INDICE24, INDICE25)");
                sb.Append(" values(");
                sb.Append(" '" + itemValue.Id + "',");
                sb.Append(" '" + itemValue.SourceId + "',");
                sb.Append(" '" + itemValue.ItemId + "',");
         
                for (int j = 0; j < itemValue.IndiceKvps.Count; j++)
                {
                    IndiceKVP kvp = itemValue.IndiceKvps[j];

                    XmlDocument doc = new XmlDocument();
                    XmlElement root = doc.CreateElement("Document");
                    XmlElement indiceName = doc.CreateElement("IndiceName");
                    indiceName.InnerText = kvp.Key;
                    root.AppendChild(indiceName);
                    XmlElement result = doc.CreateElement("Result");
                    if (kvp.ImageUrl != null)
                    {
                        result.SetAttribute("Type", "Image");
                        result.SetAttribute("Value", kvp.Id);
                        result.SetAttribute("Description", kvp.Value);
                        IndiceImage indiceImage = new IndiceImage();
                        indiceImage.Id = kvp.Id;
                        indiceImage.ImageUrl = kvp.ImageUrl;
                        DataServiceManager.ImageService.SaveOrUpdateObject(indiceImage);
                    }
                    else
                    {
                        result.SetAttribute("Type", "Number");
                        result.SetAttribute("Value", kvp.Value);
                    }
                    
                    root.AppendChild(result);
                    XmlElement unit = doc.CreateElement("Unit");
                    unit.InnerText = kvp.Unit;
                    root.AppendChild(unit);
                    XmlElement referenceValue = doc.CreateElement("ReferenceValue");
                    referenceValue.InnerText = kvp.Reference.ToString();
                    root.AppendChild(referenceValue);
                    doc.AppendChild(root);
                    sb.Append(" '" + doc.InnerXml.ToString() + "',");
                }
                for (int k = 0; k < MAX_INDICE_NUM - itemValue.IndiceKvps.Count; k++)
                {
                    if (k == MAX_INDICE_NUM - itemValue.IndiceKvps.Count - 1)
                    {
                        sb.Append(" ''");
                    }
                    else
                    {
                        sb.Append(" '',");
                    }
                }
                
                sb.Append(")");
                this.dbConn.Insert(sb.ToString());
            }
            return true;
        }

        public ObservableCollection<ExamItemValue> GetExamItemValueBySourceId(String sourceId)
        {
            ObservableCollection<ExamItemValue> itemValueList = new ObservableCollection<ExamItemValue>();
            ExamItemValue itemValue;
            StringBuilder sb = new StringBuilder();
            sb.Clear();
            sb.Append(" select *");
            sb.Append(" from DUO_EXAMITEM_VALUE");
            sb.Append(" where SOURCEID='" + sourceId + "'");

            DataTable dt = this.dbConn.Select(sb.ToString());
            if (dt != null)
            {
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    itemValue = new ExamItemValue();
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        string clmName;
                        clmName = dt.Columns[i].ColumnName;
                        itemValue.setValue(clmName, dt.Rows[j][clmName]);
                    }
                    itemValueList.Add(itemValue);
                }
                return itemValueList;
            }
            else
                return null;          
        }
    }
}
